package com.dmw.demo.controller;

import com.dmw.demo.dto.PageRequest;
import com.dmw.demo.dto.QueryRequest;
import com.dmw.demo.dto.Response;
import com.dmw.demo.service.SearcherService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("searcher")
public class SearcherController {

    @Resource
    private SearcherService searcherService;

    @RequestMapping(value = "/search")
    public String search(@RequestBody QueryRequest queryRequest, HttpServletResponse res){
        String query = queryRequest.getQuery();
        PageRequest pageRequest = queryRequest.getPageRequest();
        if(query == null || query.equals("")){
            res.setStatus(404);
            return Response.fail("搜索词不能为空！");
        }
        if(pageRequest == null){
            res.setStatus(404);
            return Response.fail("分页参数不能为空！");
        }
        long pageNo = pageRequest.getPageNo();
        long pageSize = pageRequest.getPageSize();
        if(pageNo<=0 || pageSize<=0){
            res.setStatus(404);
            return Response.fail("分页参数错误！");
        }
        return searcherService.search(query, pageNo, pageSize);
    }
}
